
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

//SVN Ref
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{

	public class SVNImport : CommandBase
	{
		java.io.File filepath;
		SVNURL dest_URL; // destination URL
		String commit_message;
		SVNProperties revision_properties;
		Boolean use_global_ignores;
		Boolean ignoreUnknown; // ignore unknown node types such as device files and pipes
		SVNDepth depth;

		public SVNImport () // Empty
		{
		}
		
		public SVNImport(java.io.File path, SVNURL dest, String cmtMsg, 
		                 SVNProperties rev_prop, bool gbl_ig, bool unknownig,
		                 SVNDepth dpth)
		{
			Debug.WriteLine("SVN Import Full blown constructor");
			this.filepath = path;
			this.dest_URL = dest;
			this.commit_message = cmtMsg;
			this.revision_properties = rev_prop;
			this.use_global_ignores = gbl_ig;
			this.ignoreUnknown = unknownig;
			this.depth = dpth;
		}
		
		public SVNImport(java.io.File path, SVNURL dest)
		{
			Debug.WriteLine("SVN Import minimal constructor");
			this.filepath = path;
			this.dest_URL = dest;
			this.commit_message = "Import";
			this.revision_properties = null;
			this.use_global_ignores = false; // since general sync tool
			this.ignoreUnknown = true;
			this.depth = SVNDepth.INFINITY;
		}
		
		private void cheapImport(string path, string dstURL) // for handleCLI
		{
			this.filepath = new java.io.File(path);
			this.dest_URL = SVNURL.parseURIEncoded(dstURL);
			this.commit_message = "Import";
			this.revision_properties = null;
			this.use_global_ignores = false; // since general sync tool
			this.ignoreUnknown = true;
			this.depth = SVNDepth.INFINITY;
			
		}
		
		public override void Execute()
		{
			Debug.WriteLine("SVN Import Execution");
			
					
			// Setup library
			Tools.setupLibrary();
			
			// Setup clients
			SVNClientManager cm = SVNClientManager.newInstance();
			SVNCommitClient cc = cm.getCommitClient();
			
			try {
				cc.doImport(filepath,dest_URL,commit_message,
				            revision_properties,use_global_ignores,
				            ignoreUnknown,depth);

			} catch (SVNException exptn) {
				throw new SVNExecuteException(exptn.getMessage());
			}
			
		}
		
		public override Dictionary<string, List<object>> getTokens ()
		{
			Dictionary<string, List<object>> ret = new Dictionary<string, List<object>> ();
			ret.Add("import",new List<object>() {"im","import"});
			return ret;
		}
		
		public override List<KeyValuePair<string,initPointer>> getInitPointers()
		{
			List<KeyValuePair<string,initPointer>> ret = new List<KeyValuePair<string,initPointer>>();
			ret.Add(new KeyValuePair<string,initPointer>("import",new initPointer(this.handleCLI)));
			return ret;
		}
		
		public void handleCLI(Dictionary<string,List<object>> tokens)
		{
			if (tokens["import"].Count!=2){
				Console.WriteLine("Invalid arguments");
				return;
			}			
			
			this.cheapImport((string)tokens["import"][0],(string)tokens["import"][1]);
			this.Execute();
		}
	}
}
